import { getServerSession } from 'next-auth';
import { authOptions } from '@/app/api/auth/auth.config';
import { prisma } from '@/lib/prisma';
import { redirect } from 'next/navigation';
import { TripForm } from '@/app/components/TripForm';
import type { Trip, Activity } from '@prisma/client';

type TripWithActivities = Trip & {
  activities: Activity[];
};

export default async function EditTripPage({
  params,
}: {
  params: { id: string };
}) {
  const session = await getServerSession(authOptions);
  
  if (!session?.user) {
    redirect('/api/auth/signin');
  }

  const trip = await prisma.trip.findUnique({
    where: { id: params.id },
    include: {
      activities: {
        orderBy: {
          date: 'asc',
        },
      },
    },
  });

  if (!trip) {
    return (
      <div className="container mx-auto px-4 py-8">
        <div className="bg-gray-800 rounded-lg shadow-lg p-6">
          <p className="text-red-400">行程不存在</p>
        </div>
      </div>
    );
  }

  // 检查是否是行程创建者
  if (trip.userId !== session.user.id) {
    return (
      <div className="container mx-auto px-4 py-8">
        <div className="bg-gray-800 rounded-lg shadow-lg p-6">
          <p className="text-red-400">您没有权限编辑此行程</p>
        </div>
      </div>
    );
  }

  return (
    <div className="container mx-auto px-4 py-8">
      <h1 className="text-2xl font-bold text-white mb-8">编辑行程</h1>
      <TripForm initialData={trip as TripWithActivities} />
    </div>
  );
} 